package com.example.demo.service.impl;



import com.example.demo.entity.*;
import com.example.demo.pojo.PlannerResult;
import com.example.demo.repository.*;
import com.example.demo.service.NoticeService;
import com.example.demo.time.Executetime;
import com.example.demo.vo.NoticeVo;
import com.example.demo.vo.Notice_temlateVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;


@Service
public class NoticeServiceImpl implements NoticeService {

    @Autowired
    NoticeRepository noticeRepository;

    @Autowired
    Notice_temlateRepository noticeTemlateRepository;

    @Autowired
    Notice_recptionRepository noticeRecptionRepository;

    @Autowired
    UserRepository userRepository;

    @Autowired
    JoinerRepository joinerRepository;
    @Autowired
    private ActivityRepository activityRepository;

    @Override
    public PlannerResult<String> create_notice_template(Notice_temlateVo noticeTemlateVo) {
        Notice_temlate noticeTemlate = new Notice_temlate();
        noticeTemlate.setNotice_type(noticeTemlateVo.getNotice_type());
        noticeTemlate.setContext(noticeTemlateVo.getContext());
        Notice_temlate result = noticeTemlateRepository.save(noticeTemlate);
        if (result.getId()!=null)
        return PlannerResult.success("模板创建成功");
        else return  PlannerResult.error("，模板创建失败");
    }

    @Override
    public PlannerResult<String> update_notice_template(Notice_temlateVo noticeTemlateVo) {
        Notice_temlate noticeTemlate = new Notice_temlate();
        noticeTemlate.setId(noticeTemlateVo.getId());
        noticeTemlate.setNotice_type(noticeTemlateVo.getNotice_type());
        noticeTemlate.setContext(noticeTemlateVo.getContext());
        Notice_temlate result = noticeTemlateRepository.save(noticeTemlate);
        if (result.getId()!=null)
            return PlannerResult.success("模板创建成功");
        else return  PlannerResult.error("，模板创建失败");
    }

    @Override
    public PlannerResult<String> delete_notice_template(Integer noticetempid) {
        noticeTemlateRepository.deleteById(noticetempid);
        return PlannerResult.success("删除完成");
    }

    //创建通知
    @Autowired
    Executetime executetime;

    @Override
    public PlannerResult<String> create_notice(NoticeVo noticeVo) {
        Notice notice = new Notice();
        notice.setNotice_type_id(noticeVo.getNotice_type_id());
        notice.setContext(noticeVo.getContext());
        notice.setActivity_id(noticeVo.getActivity_id());
        notice.setSend_time(noticeVo.getSend_time());
        Notice result = noticeRepository.save(notice);

        //该活动参与者列表
        List<Joiner> joiners = joinerRepository.findByActivity_id(notice.getActivity_id()) ;
//        System.out.println("*******************************************");
//        System.out.println(joiners);
//        System.out.println("*******************************************");
        List<Integer> ss = new ArrayList<>();

        for (Joiner join: joiners
             ) {
            ss.add(join.getUser_id());
        }
//        System.out.println("*******************************************");
//        System.out.println(ss);
//        System.out.println("*******************************************");

        if(result.getId()!=null)
        {
            for (Integer s: ss
                 ) {

                User user = userRepository.findById(s).get();
//                System.out.println("***********User*****************");
//                System.out.println(user);

                if(!user.getId().equals(activityRepository.findById(noticeVo.getActivity_id()).get().getCreator_id())){
                    executetime.timing_notice(notice.getSend_time(),notice,user);

                    //建立通知接受表项
                    Notice_recption noticeRecption = new Notice_recption();
                    noticeRecption.setNotice_id(result.getId());
                    noticeRecption.setUser_id(s);
                    noticeRecption.setActivity_id(notice.getActivity_id());
                    noticeRecption.setState("未读");
                    noticeRecptionRepository.save(noticeRecption);
                }
            }
            return PlannerResult.success("创建通知成功");
        }
        else {
            return PlannerResult.error("创建通知失败");
        }


    }

    //统计所有通知接收情况

    @Override
    public PlannerResult<List<Integer>> count_notice(Integer activity_id) {
        List<Integer> result = new ArrayList<>();
        List<Notice_recption> xxx = noticeRecptionRepository.findAllByActivityId(activity_id);
        Integer count = 0;
        Integer sum = xxx.size();
        for (Notice_recption xx: xxx
             ) {
            if(xx.getState().equals("已接收"))
            count=count+1;
        }
        result.add(count);
        result.add(sum);
        return PlannerResult.success(result);
    }


}
